package com.duoduo.demo.springnoxml;

import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.duoduo.demo.springnoxml.websocket.ChatWebSocketHandler;
import com.duoduo.demo.springnoxml.websocket.WebSocketHandshakeInterceptor;

/**
 * @url http://www.cnblogs.com/nosqlcoco/p/5860730.html
 * @author chengesheng@kedacom.com
 * @date 2017年11月3日下午5:02:43
 */
// @Configuration
// @EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		// 允许连接的域,只能以http或https开头
		String[] allowsOrigins = {
				"http://www.xxx.com"
		};

		// WebIM WebSocket通道
		registry.addHandler(chatWebSocketHandler(), "/webSocketIMServer").setAllowedOrigins(allowsOrigins)
				.addInterceptors(myInterceptor());
		registry.addHandler(chatWebSocketHandler(), "/sockjs/webSocketIMServer").setAllowedOrigins(allowsOrigins)
				.addInterceptors(myInterceptor()).withSockJS();
	}

	@Bean
	public ChatWebSocketHandler chatWebSocketHandler() {
		return new ChatWebSocketHandler();
	}

	@Bean
	public WebSocketHandshakeInterceptor myInterceptor() {
		return new WebSocketHandshakeInterceptor();
	}
}
